package org.csu.molediary.api.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Friend;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class AccountController {

    Account account;

    @Autowired
    private AccountService accountService;

    //登录
    @PostMapping("login")
    @ResponseBody
    public CommonResponse<Account> loginAccount(
            @RequestParam("id")String id,
            @RequestParam("sex")String sex,
            @RequestParam("name") String name,
            @RequestParam("head") String head,
            @RequestParam("month") int month,
            @RequestParam("sign") String sign,
            @RequestParam("date") String date,
            @RequestParam("location") String location){
        //如果传入id未被使用
        if(accountService.selectAccountById(id).getMsg().equals("没有该用户")){
            //设置初始被点赞数
            int accountgoodnum=0;

            account.setId(id);
            account.setSex(sex);
            account.setName(name);
            account.setHead(head);
            account.setMonth(month);
            account.setSign(sign);
            account.setDate(date);
            account.setAccountgoodnum(accountgoodnum);
            account.setLocation(location);

            //插入Account表
            return accountService.insertNewAccount(account);
        }
        //传入id已经被使用
        else
            return accountService.selectAccountById(id);

    }

    @GetMapping("information/{userid}")
    @ResponseBody
    public CommonResponse<Account> selectAccountById(@PathVariable("userid")String userid){

        return accountService.selectAccountById(userid);
    }

    //修改更新信息
    @PutMapping("updateinformation")
    @ResponseBody
    public CommonResponse<Account> updateAccountInformation(
            @RequestParam("id")String id,
            @RequestParam("sex")String sex,
            @RequestParam("name") String name,
            @RequestParam("head") String head,
            @RequestParam("month") int month,
            @RequestParam("sign") String sign,
            @RequestParam("date") String date,
            @RequestParam("location") String location
    ){
        //获取到原来的account信息
        CommonResponse<Account> accountCommonResponse=accountService.selectAccountById(id);

        accountCommonResponse.getData().setSex(sex);
        accountCommonResponse.getData().setName(name);
        accountCommonResponse.getData().setHead(head);
        accountCommonResponse.getData().setMonth(month);
        accountCommonResponse.getData().setSign(sign);
        accountCommonResponse.getData().setDate(date);
        accountCommonResponse.getData().setLocation(location);

        //返回修改后的信息
        return accountService.updateAccountInformation(accountCommonResponse.getData());
    }

    //模糊查询用户的名字来得到用户信息
    @GetMapping("namelike/{namelike}")
    @ResponseBody
    public CommonResponse<List<Account>> getAccountByNameLike(@PathVariable("namelike")String namelike){

        return accountService.getAccountByNameLike(namelike);
    }
}
